package com.beirtipol.travel.sax;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.AttributeList;
import org.xml.sax.SAXException;

import com.beirtipol.travel.activities.dom.Line;
import com.beirtipol.travel.activities.dom.LineStatus;
import com.beirtipol.travel.activities.dom.LineStatus.ServiceClass;

public class StatusHandler extends FeedHandler
{
	private Map<Line, LineStatus>	lineStatusMap	= new HashMap<Line, LineStatus>();

	private Line					currentLine;
	private LineStatus				currentStatus;

	public Map<Line, LineStatus> getLineStatusMap()
	{
		return lineStatusMap;
	}

	@Override
	public void endElement(String name) throws SAXException
	{
		if ("LineStatus".equals(name))
		{
			lineStatusMap.put(currentLine, currentStatus);
			currentLine = null;
			currentStatus = null;
		}
	}

	@Override
	public void startElement(String name, AttributeList attributes) throws SAXException
	{
		if ("LineStatus".equals(name))
		{
			currentStatus = new LineStatus();
			currentStatus.setStatusDetails(attributes.getValue("StatusDetails"));
		}
		else if ("Line".equals(name))
		{
			currentLine = Line.parse(attributes.getValue("Name"));
		}
		else if ("Status".equals(name))
		{
			currentStatus.setStatus(ServiceClass.parse(attributes.getValue("ID")));
			currentStatus.setDescription(attributes.getValue("Description"));
			currentStatus.setActive(Boolean.getBoolean(attributes.getValue("IsActive")));
		}
		super.startElement(name, attributes);
	}

	@Override
	public String getURL()
	{
		return "http://cloud.tfl.gov.uk/TrackerNet/LineStatus";
	}

}
